package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.aliyun.oss.OSS;
import com.example.demo.configuration.AliyunOssConfig;
import com.example.demo.constant.RedisConstant;
import com.example.demo.constant.SystemConstant;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.RankDTO;
import com.example.demo.mapper.entity.Student;
import com.example.demo.mapper.entity.StudentCertification;
import com.example.demo.mapper.entity.StudentPoints;
import com.example.demo.service.StudentCertificationService;
import com.example.demo.service.StudentLoginService;
import com.example.demo.service.StudentPointsService;
import com.example.demo.service.TestService;
import com.example.demo.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@RequestMapping("testController")
@Api(value = "测试接口", tags = {"测试接口"})
@Slf4j
@RestController
public class TestController {
    @Autowired
    private TestService service;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private StudentCertificationService studentCertificationService;

    @Autowired
    private StudentPointsService studentPointsService;

    private OSS ossClient;

    //提交免冠照片
    @ApiOperation(value = "上传免冠照片")
    @PostMapping("/uploadBareheaded")
    public Result uploadBareheaded(@RequestParam("studentId") int studentId, @RequestParam("file") MultipartFile file) throws IOException {

        Student currentStudent = service.selectStudentById(studentId);

        log.info(file.toString());

        String originalFileName = file.getOriginalFilename();//从上传的文件中获取原始文件名
        int i = originalFileName.lastIndexOf(".");
        String png = originalFileName.substring(i);//获取文件扩展名

        //存储空间的名字
        String bucketName = "seedling-classmate";

        //创建存储对象
        ossClient = AliyunOssConfig.ossClient();

        //设置文件名并上传到OSS
        String fileName = "bareheaded/id-" +studentId+"-"+ UUID.randomUUID().toString().substring(0,12)+png;
        ossClient.putObject(bucketName, fileName, file.getInputStream());   //之后在数据库中保存fileName

        StudentCertification bareheaded = new StudentCertification();
        bareheaded.setStudentNumber(currentStudent.getNumber());
        bareheaded.setBareheadedPhoto(fileName);

        service.placeBareheadedPhoto(bareheaded);
        ossClient.shutdown();

        String result = "http://seedling-classmate.oss-cn-beijing.aliyuncs.com/" + fileName;
        return Result.ok("上传成功", result);
    }

    @ApiOperation("创建积分排行榜")
    @RequestMapping(value = "createLeaderboard", method = {RequestMethod.POST})
    public Result<Object> createLeaderboard(){
        List<StudentPoints> studentPointsList = studentPointsService.selectStudentPointsList();
        for(StudentPoints studentPoints : studentPointsList){
            redisUtil.addOrUpdate(RedisConstant.Student_Points_Rank_Name, studentPoints.getStudentId(), studentPoints.getTotalEarned());
        }
        return Result.ok("创建成功");
    }

    @ApiOperation("获取积分排行榜")
    @RequestMapping(value = "getLeaderboard", method = {RequestMethod.POST})
    public Result<Object> getLeaderboard(){
        Set<Object> topTenRank = redisUtil.top(RedisConstant.Student_Points_Rank_Name,10);
        System.out.println("从redis中直接读取的排行榜数据"+topTenRank);
        return Result.ok("创建成功");
    }

}
